<?php
namespace lib;
class MenuButton{
    public $type;//菜单类型
    public $name;//菜单名称
    public $key;//菜单key
    public $url;//菜单url
    public $sub_button;//二级菜单数组

    public function __construct($name,$type,$key=null,$url=null,$sub_button=null){
        $this->name = $name;
        $this->type = $type;
        $this->key = $key;
        $this->url = $url;
        if(!empty($sub_button)){
            $this->sub_button = $sub_button;
        }
    }

    public function toArray(){
        $arr = array();
        if(count($this->sub_button) > 0){
            foreach($this->sub_button as $button){
                array_push($arr,$button->toArray());
            }
        }
        $data = array();
        $data['name'] = urlencode($this->name);
        switch($this->type){
            case 'click':
                $data['type'] = $this->type;
                $data['key'] = $this->key;
                break;
            case 'view':
                $data['type'] = $this->type;
                $data['url'] = $this->url;
                break;
            case 'scancode_push':
            case 'scancode_waitmsg':
            case 'pic_sysphoto':
            case 'pic_photo_or_album':
            case 'pic_weixin':
            case 'location_select':
                $data['type'] = $this->type;
                $data['key'] = $this->key;
                break;
            case 'media_id':
            case 'view_limited':
                $data['type'] = $this->type;
                $data['media_id'] = $this->key;
                break;
        }
        if(count($arr) > 0){
            $data['sub_button'] = $arr;
        }
        return $data;
    }
}